#提取四大类里面包含的细分垃圾文件夹的照片，并重新编号到四大类中，删除原先的子文件夹，
import os
import shutil


def extract_and_rename_images(root_folder):
	for main_category_folder in os.listdir(root_folder):
		main_category_path = os.path.join(root_folder, main_category_folder)
		
		if os.path.isdir(main_category_path):
			for sub_category_folder in os.listdir(main_category_path):
				sub_category_path = os.path.join(main_category_path, sub_category_folder)
				
				if os.path.isdir(sub_category_path):
					image_files = [f for f in os.listdir(sub_category_path) if
					               f.lower().endswith(('.png', '.jpg', '.jpeg', '.gif'))]
					
					for i, image_file in enumerate(image_files):
						image_path = os.path.join(sub_category_path, image_file)
						new_name = f"{main_category_folder} {i + 1}.jpg"  # Adjust the file extension as needed
						destination_path = os.path.join(main_category_path, new_name)
						
						shutil.move(image_path, destination_path)
					
					# Remove the sub-category folder
					shutil.rmtree(sub_category_path)


if __name__ == "__main__":
	root_directory = "./dataSet_good3"  # Replace with the actual path
	extract_and_rename_images(root_directory)

